<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="bank" />
            <i class="el-icon-office-building" />
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主体 -->

        <el-tree
          default-expand-all
          :data="data"
          icon-class=" "
          @node-click="handleNodeClick"
        >
          <template slot-scope="{data,node}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon
                  v-if="data.children.length !== 0"
                  :icon-class="node.expanded ? 'minus' : 'add'"
                />
                <svg-icon v-else icon-class="user1" />
                <i class="el-icon-s-custom" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length===0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <el-dialog
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      :title=" isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
    >
      <DeptDialog
        :id="curId"
        ref="deptDialog"
        :isedit="isEdit"
        :origin-lis="originList"
        @success="hSuccess"
        @close="hClose"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/treeList.js'

import DeptDialog from './deptDialog.vue'
export default {
  name: 'Departments',
  components: { DeptDialog },
  data() {
    return {
      curId: '', // 当前操作的部门的id
      data: [],
      showDialog: false,
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 子组件抛出了success事件
    hSuccess() {
      // 1. 关闭弹层
      this.showDialog = false
      // 2. 重新渲染
      this.loadDepartments()
    },
    // 用户点击了添加子部门
    hAdd(id) {
      // 显示弹层
      this.showDialog = true
      // 更新当前操作的id
      this.curId = id
      this.isEdit = false
      // 获取子组件的引用
      this.$nextTick(() => {
        console.log('获取子组件的引用', this.$refs.deptDialog)
        console.log('当前的curId', id)
        console.log('获取到子组件中的id--从父传入的', this.$refs.deptDialog.id)
        // 调用子组件的方法
        this.$refs.deptDialog.loadDetail()
      })
    },
    async loadDepartments() {
      const res = await getDepartments()
      console.log(res, 444)
      // 保存

      // 第一个元素是公司的名字，所以去掉
      // 删除数组的第一个元素 shift
      res.data.depts.shift()
      this.originList = res.data.depts
      // 调用提前封装好的工具函数来做格式转换
      this.data = tranListToTreeData(res.data.depts)
    },
    handleNodeClick(data) {
      console.log(data)
    },
    // 编辑功能
    hEdit(id) {
      this.showDialog = true
      this.isEdit = true
      this.curId = id
      // 获取子组件的引用
      this.$nextTick(() => {
        console.log('获取子组件的引用', this.$refs.deptDialog)
        console.log('当前的curId', id)
        console.log('获取到子组件中的id--从父传入的', this.$refs.deptDialog.id)
        // 调用子组件的方法
        this.$refs.deptDialog.loadDetail()
      })
    },
    // 用户点击删除
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    // 删除功能
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    },
    hClose() {
      this.showDialog = false
    }

  }

}
</script>
